package com.dz.finance.wechat.job;

import com.dz.finance.wechat.config.WeChatConfig;
import com.dz.finance.wechat.service.WeChatService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author: DengHaoQi
 * @date: 2024/7/1 14:40
 * @description: 定时任务获取access_token
 */


@Slf4j
@Component
@RequiredArgsConstructor
public class WeChatXxlJob {

    private final WeChatService weChatService;
    private final WeChatConfig weChatConfig;

    @XxlJob("setMpAccessTokenCacheJobHandler")
    public ReturnT<String> setMpAccessTokenCacheJobHandler(String param) throws Exception {
        XxlJobHelper.log("setBrandConfigCacheJobHandler -> begin");
        try {
            XxlJobHelper.log("开始执行任务");
            weChatService.setAccessTokenInCache(weChatConfig.getMp().getAppId(), weChatConfig.getMp().getAppSecret());
            XxlJobHelper.log("任务执行结束");
        } catch (Exception e) {
            XxlJobHelper.log("任务执行失败", e);
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }
}
